package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Override
    public ResponseResult findAll() {
        //之前的方法
//        return ResponseResult.okResult(list());

        //1构造分页 (这一步主要就是为了安全考虑，限制这个接口查询数据的最大值)
        Page<WmChannel> page = new Page<>(1, 100);
        //2列表查询
        Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(page, null);

        //3 返回数据
        return ResponseResult.okResult(wmChannelPage.getRecords());
    }

    @Override
    public ResponseResult channelSave(WmChannel channel) {
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        String name = channel.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "频道名缺失");
        }
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName, name);
        Integer count = wmChannelMapper.selectCount(wrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名已经存在不能重复");
        }
        channel.setCreatedTime(new Date());
        boolean save = this.save(channel);

        return save ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.okResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult channelList(ChannelDto dto) {
        if (dto == null) {
            return new PageResponseResult(0, 0, 0);
        }
        Integer page = dto.getPage();
        Integer size = dto.getSize();

        page = page == null ? 1 : page;
        size = size == null ? 10 : size;
        //构建分页
        Page<WmChannel> pageInfo = new Page<>(page, size);

        String name = dto.getName();
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        //模糊查询
        wrapper.like(StringUtils.isNotBlank(name),WmChannel::getName,name);
        wrapper.orderByDesc(WmChannel::getCreatedTime);

        Page<WmChannel> pageResult = wmChannelMapper.selectPage(pageInfo, wrapper);
        //返回结果
        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page, size, total.intValue());
        pageResponseResult.setData(pageResult.getRecords());



        return pageResponseResult;
    }

    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Override
    public ResponseResult channelUpdate(WmChannel wmChannel) {
        //参数判空
        if (wmChannel == null || wmChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,wmChannel.getId())
                .eq(WmNews::getStatus,WmNews.Status.PUBLISHED.getCode());

        //判断是否被引用
        Integer count = wmNewsMapper.selectCount(wrapper);
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道被引用不能修改或禁用");
        }
        //2 修改
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {

        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel wmChannel = getById(id);
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"有效频道，不能删除");
        }

        //判断是否被引用
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId, wmChannel.getId());
        Integer count = wmNewsMapper.selectCount(wrapper);
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道被引用不能删除");
        }

        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}